package com.hbx.utils;

import com.hbx.constant.ResponseStateConstant;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/*
 * @Author:hbx
 *
 * */
@ApiModel(value = "结果", description = "统一结果处理Bean")
public class Result<T> {
    @ApiModelProperty(value = "状态码")
    private Integer code;
    @ApiModelProperty(value = "信息")
    private String msg;
    @ApiModelProperty(value = "数据")
    private T data;

    public Result() {
    }

    public Result(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public Result setCode(Integer code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public Result setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public T getData() {
        return data;
    }

    public Result setData(T data) {
        this.data = data;
        return this;
    }

    /**
     * 操作成功
     *
     * @return
     */
    public static Result ok() {
        Result r = new Result();
        r.setMsg("请求成功");
        r.setCode(ResponseStateConstant.OPERA_SUCCESS);
        return r;
    }

    public static Result ok(String msg) {
        Result r = new Result();
        r.setCode(ResponseStateConstant.OPERA_SUCCESS);
        r.setMsg(msg);
        return r;
    }

    /**
     * 操作失败
     *
     * @return
     */
    public static Result error() {
        Result r = new Result();
        r.setCode(ResponseStateConstant.OPERA_FAIL);
        return r;
    }

    public static Result error(String msg) {
        Result r = new Result();
        r.setCode(ResponseStateConstant.OPERA_FAIL);
        r.setMsg(msg);
        return r;
    }

    @Override
    public String toString() {
        return "Result{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}
